package org.lotterm.jconcat;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BadPdfFormatException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;

public class Concat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length!=3) {
			System.out.println("Wrong number of arguments!");
			return;
		}
		
		try {
			PdfReader reader1 = new PdfReader(args[0]);
			PdfReader reader2 = new PdfReader(args[1]);
			
			Document document = new Document();

			PdfCopy copy;
			try {
				copy = new PdfCopy(document, new FileOutputStream(args[2]));
			} catch (DocumentException e) {
				System.out.println("A document exception occured!");
				e.printStackTrace();
				return;
			}
			System.out.println("It just works like a zip.");
			document.open();

			int n=reader1.getNumberOfPages();

			for (int page = 1; page <= n; page++ ) {
	            copy.addPage(copy.getImportedPage(reader1, page));
	            copy.addPage(copy.getImportedPage(reader2, n-(page-1)));
	        }

			document.close();
			System.out.println("Done.");
		} catch (IOException e) {
			System.out.println("Can not access file!");
			e.printStackTrace();
			return;
		} catch (BadPdfFormatException e) {
			System.out.println("Bad format!");
			e.printStackTrace();
			return;
		}

		
	}

}
